package com.hadoopSpark.application.test;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.net.ssl.SSLContext;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;

import com.aliyun.configuration.ConfigurationTool;
import com.certificate.tool.SetTrustStore;
import com.common.constant.Constant;

/*
 * 在家里，进一步，整理修改
 * 在阿里云上，创建一个hadoop应用,
 * 测试，
 * 1、compose yaml 字符串  中传入参数
 * 2、测试创建3个节点
 * 3、想想如何根据用户传入的集群个数，来确定
 */
public class CreateHadoopAppDemo002 {
	
	public static void main(String[] args) {
	
		 //get certificate address
		 try {
			 SSLConnectionSocketFactory sslsf = SetTrustStore.getSSLConnection();
			 //httpclient链接
			 CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			 
			 HttpPost httpPost = new HttpPost(ConfigurationTool.getProperties(Constant.ALIYUN_PROJECTS_URL));
			 
			 httpPost.addHeader("Content-type","application/json;charset=UTF-8");
			 
			 
			 String slaveYaml = "slave1:\\r\\n  image: 'registry.aliyuncs.com/registry_yxkj/hadoop_spark:1.0'\\r\\n  hostname: 'slave1-hadoop'\\r\\n  ports:\\r\\n    - 80\\r\\n    - 22\\r\\n    - 9000\\r\\n  environment:\\r\\n    - isForSpark=yes\\r\\n    - isForHive=yes\\r\\n  volumes:\\r\\n    - '/dataValue2/slave1-Hadoop:/usr/local'\\r\\n  restart:  always\\r\\n";
			 
			 StringBuffer composeYaml = new StringBuffer();
			 //下面修改的是，master 容器的 编排模板
			 composeYaml.append(ConfigurationTool.getProperties(Constant.MASTER_Container_NAME_YAML))
				.append("  "+ConfigurationTool.getProperties(Constant.IMAGE_YAML))
				.append("  "+ConfigurationTool.getProperties(Constant.HOSTNAME_YAML)+ConfigurationTool.getProperties(Constant.MASTER_HOSTNAME_YAML)+ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
				.append("  "+ConfigurationTool.getProperties(Constant.PORTS_YAML))
				.append("  "+ConfigurationTool.getProperties(Constant.ENVIRONMENT_YAML))
				.append("  "+ConfigurationTool.getProperties(Constant.RESTART_YAML))
				.append("  "+ConfigurationTool.getProperties(Constant.VOLUMES_YAML)+ConfigurationTool.getProperties(Constant.MASTER_VOLUME_YAML))
				.append(ConfigurationTool.getProperties(Constant.VOLUMES_TO_CONYAML))
				.append("  "+ConfigurationTool.getProperties(Constant.OTHER_ARGS_YAML))
				.append(ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
				;
			 
			 int nodeNum = Integer.valueOf(args[0]); 
			 for(int i = 1; i < nodeNum; i++ ){
				 
				 composeYaml.append(ConfigurationTool.getProperties(Constant.SLAVE_CONTAINER_NAME_YAML)+i+":"+ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
				 			.append("  "+ConfigurationTool.getProperties(Constant.IMAGE_YAML))
				 			.append("  "+ConfigurationTool.getProperties(Constant.HOSTNAME_YAML)+ConfigurationTool.getProperties(Constant.SLAVE_HOSTNAME_YAML)+i+ConfigurationTool.getProperties(Constant.SLAVE_HOSTNAME_SUFFIX_YAML)+ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
				 			.append("  "+ConfigurationTool.getProperties(Constant.PORTS_YAML))
				 			.append("  "+ConfigurationTool.getProperties(Constant.ENVIRONMENT_YAML))
				 			.append("  "+ConfigurationTool.getProperties(Constant.RESTART_YAML))
				 			.append("  "+ConfigurationTool.getProperties(Constant.VOLUMES_YAML)+ConfigurationTool.getProperties(Constant.SLAVE_HOSTNAME_YAML)+i)
				 			.append(ConfigurationTool.getProperties(Constant.VOLUMES_TO_CONYAML))
				 			.append("  "+ConfigurationTool.getProperties(Constant.OTHER_ARGS_YAML))
				 			.append(ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
				 			;
			 }
			 
			 String autoYaml = "\""+composeYaml.toString()+"\"";
			 
			 System.out.println("--------\n"+autoYaml);
			 
			 //下面这个，已经完全OK了，其他，可以删掉了
			 String composeYamlMode1 = 
					 "\"master:\\r\\n  image: 'registry.aliyuncs.com/registry_yxkj/hadoop_spark:1.0'\\r\\n  hostname: 'master-hadoop'\\r\\n  ports:\\r\\n    - 80\\r\\n    - 22\\r\\n    - 9000\\r\\n  environment:\\r\\n    - isForSpark=yes\\r\\n    - isForHive=yes\\r\\n  volumes:\\r\\n    - '/dataValue2/master-Hadoop:/usr/local'\\r\\n  restart:  always\\r\\n"
					 + slaveYaml
					 + "slave2:\\r\\n  image: 'registry.aliyuncs.com/registry_yxkj/hadoop_spark:1.0'\\r\\n  hostname: 'slave2-hadoop'\\r\\n  ports:\\r\\n    - 80\\r\\n    - 22\\r\\n    - 9000\\r\\n  environment:\\r\\n    - isForSpark=yes\\r\\n    - isForHive=yes\\r\\n  volumes:\\r\\n    - '/dataValue2/slave2-Hadoop:/usr/local'\\r\\n  restart:  always\"";
			 System.out.println("=================");
			 System.out.println(composeYamlMode1);
			 System.out.println("=================");
			 
			 //这里还有问题------实现   请求体的 名称， 描述， 版本号 的动态化， 还未做
			 String appName = args[1];
			 String appDesc = args[2];
			 String  requestEntity =
					 "{\"name\":"+"\""+appName+"\","
					+ "\"description\":"+"\""+appDesc+"\","
					+ "\"template\":"+autoYaml+","
					+ "\"version\":\"1.0\","
					+ "\"environment\":{\"isForSpark\":\"no\",\"nodesNum\":\"9\"}}";
			 System.out.println(requestEntity);
			 
			 StringEntity stringEntity = new StringEntity(requestEntity, Charset.forName("UTF-8"));
			 
			 httpPost.setEntity(stringEntity);

			 //使用客户端向服务器发送数据
			 HttpResponse httpResponse = httpClient.execute(httpPost);
			 
			 if (httpResponse.getStatusLine().getStatusCode() == 201) {
				HttpEntity resultEntity = httpResponse.getEntity();
				
				//返回结果是，服务器上，返回来的是json字符串数据
				String strResultEntity = EntityUtils.toString(resultEntity);
				System.out.println(strResultEntity);
				System.out.println("SB--------------------> OK");
			} else {
				HttpEntity resultEntity = httpResponse.getEntity();
				//返回结果是，服务器上，返回来的是json字符串数据
				String strResultEntity = EntityUtils.toString(resultEntity);
				System.out.println(strResultEntity);
				System.out.println(httpResponse.getStatusLine().getStatusCode());
				System.out.println("SB--------------------> 错啦");
			}
		
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
}
		
		
		
